threads.start(function(){
    xuanfu()
})
threads.start(function(){
    while(true){
        关闭广告()
    }
})
检测app("雇客","https://www.guke991.com/register.html?activeCode=13452210513")
wait("首页")
sleep(900)
点击不可点击id("getRedEnvelopeImg")
sleep(3500)
while(true){
    点击不可点击文本("立即抢")
    if(text("已领取").findOnce()){
        sleep(900)
        console.log("看广告结束，即将退出程序。。。")
        break
    }
}
engines.stopAll()


function 关闭广告(){
    点击不可点击id("closeImg")
    点击不可点击文本("| 跳过")
    点击不可点击文本("跳过")
    点击不可点击id("ksad_end_close_btn")
    点击不可点击id("tt_video_ad_close_layout")
    点击不可点击id("tt_video_ad_close")
    点击不可点击id("tt_insert_dislike_icon_img")
    点击不可点击文本("取消")
    if(className("android.widget.ImageView").depth(5).drawingOrder(2).indexInParent(1).findOnce()){
        console.log("关闭广告")
        sleep(900)
        className("android.widget.ImageView").depth(5).drawingOrder(2).indexInParent(1).click()
        sleep(1500)
    }
}


//等待出现模块
function wait(Name) {
    console.log("等待->",Name)
    text(Name).waitFor()
    sleep(1000)
}
function 检测app(name,url,str){
    var appname = getPackageName(name)
    if(appname == null){
        toastLog("未安装该应用")
        sleep(1500)
        var download = confirm("是否去安装"+ name +"?")
        if(download){
            app.openUrl(url)
            toastLog("正在去下载。。。")
            sleep(1500)
            toastLog("支持作者记得填下邀请码哦！")
            setClip(str)
            sleep(2000)
            toastLog("邀请码已复制到剪切板")
            sleep(1500)
        }
    } else {
        toastLog("正在打开" + appname )
        sleep(500)
        launchApp(name)
        sleep(8000)
    }
}
//悬浮窗
//=====================================================
function xuanfu() {
    var window = floaty.window(
        <frame padding="1">
            <vertical  >
                <horizontal>
                    <button id="action" text="点击" w="35" h="50" style="Widget.AppCompat.Button.Colored" textSize="12sp" />
                    <button id="begin" text="日志" w="35" h="50" textSize="12sp" />
                    <button id="end" text="结束" w="35" h="50" textSize="12sp" />
                    <button id="times" text="计时" w="35" h="50" textSize="12sp" />
                </horizontal>

                <com.stardust.autojs.core.console.ConsoleView h="150" w="180" id="console" bg="#80000000" />


            </vertical>
        </frame>

    );

    window.console.setConsole(runtime.console);
    window.setPosition(0, device.height / 5 * 4);
    // window.console.findViewById(org.autojs.autojs.R.id.input_container).setVisibility(android.view.View.GONE);
    window.console.attr("visibility", "gone");
    
    //开始计时
    var before = new Date();

    //日志和结束按钮不显示
    window.begin.setVisibility(8)
    window.end.setVisibility(8)
    window.times.setVisibility(8)
    // window.console.setTouchable(false);

    var x = 0, y = 0;
    var windowX, windowY;
    var downTime;
    window.action.setOnTouchListener(function (view, event) {
        switch (event.getAction()) {
            case event.ACTION_DOWN:
                x = event.getRawX();
                y = event.getRawY();
                windowX = window.getX();
                windowY = window.getY();
                downTime = new Date().getTime();
                return true;
            case event.ACTION_MOVE:
                //移动手指时调整悬浮窗位置
                window.setPosition(windowX + (event.getRawX() - x),
                    windowY + (event.getRawY() - y));
                return true;
            case event.ACTION_UP:
                //手指弹起时如果偏移很小则判断为点击
                if (Math.abs(event.getRawY() - y) < 5 && Math.abs(event.getRawX() - x) < 5) {
                    onClick();
                }
                return true;
        }
        return true;
    });

    function onClick(str, sec) {

        if (window.end.visibility == 8) {
            window.console.setVisibility(0)
            window.end.setVisibility(0)
            window.times.setVisibility(0)
        } else {
            window.console.setVisibility(8)
            window.end.setVisibility(8)
            window.times.setVisibility(8)
        }
    }

    var timerId;

    function 显示状态信息(str, sec) {
        try {
            clearTimeout(timerId);
        }
        catch (error) {
            toastLog("error");
        }
        ui.run(() => {
            window.action.setText(str);
        });
        timerId = setTimeout(function () {
            ui.run(() => {
                window.action.setText("");
            });
        }, sec * 1000);
    }


    window.begin.click(() => {
        window.begin.text() == "显示日志"
        if (window.begin.text() == "显示日志") {
            window.begin.setText("隐藏日志")
            window.console.attr("visibility", "visible");
        } else {
            window.begin.setText("显示日志")
            window.console.attr("visibility", "gone");
        }
    });

    window.end.click(() => {
        floaty.closeAll();
        engines.stopAll();
        exit();
    });
    window.times.click(() => {
        var now =new Date();
        var last = Math.floor((now - before) / 1000)
        if(last >= 60) {
            var minutes =  Math.floor(last / 60)
        } else {
            var minutes = 0
        }
        var seconds = last - minutes * 60
        if(minutes > 0 && seconds < 10){
            console.log("运行了：" + minutes + "分 " + "0" + seconds + "秒")
        } 
        if(minutes > 0 && seconds > 10){
            console.log("运行了：" + minutes + "分 "  + seconds + "秒")
        }
        if(minutes <= 0 && seconds < 10){
            console.log("运行了：" + "0" + seconds + "秒")
        } 
        if(minutes <= 0 && seconds > 10){
            console.log("运行了："  + seconds + "秒")
        } 
    });

}
//悬浮窗结束-----------
function 点击文本(str){
    if(text(str).findOnce()){
        sleep(2000)
        if(text(str).findOnce()){
            console.log("点击->"+str)
            text(str).click()
            sleep(900)
        }
        
    }
}
function 点击不可点击文本(str){
    if(text(str).findOnce()){
        sleep(2000)
        if(text(str).findOnce()){
            var strposition = text(str).findOnce().bounds()
            sleep(2000)
            console.log("点击->"+str)
            click(strposition.centerX(),strposition.centerY())
            sleep(900)
        }
    }
}
function 点击不可点击id(str){
    if(id(str).findOnce()){
        sleep(2000)
        if(id(str).findOnce()){
            var strposition = id(str).findOnce().bounds()
            sleep(2000)
            console.log("点击->"+str)
            click(strposition.centerX(),strposition.centerY())
            sleep(900)
    }
    }
}